<?php

require_once 'classes/LightPressPlugin.php';

class TopRead extends LightPressPlugin {
    
    var $constructor_args = array(
        'active_days'=>'number of days to query',
        'date_format'=>'custom date format',
        'limit'=>'number of posts to show');
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Display the top n most accessed posts';
    
    var $active = true;
    var $hooks = array('sidebar');

    var $active_days = 60;
    var $date_format;
    var $limit = 5;
    var $base = '/~ludo/archive/2005/04/';
    var $suffix = '.html';
    
    function TopRead(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$hook_data) {
        $db =& $this->_frontend->db;
        $tpl =& $this->_frontend->tpl;
        $messages =& $this->_frontend->messages;
        $str_index = strlen($this->base);
        $str_sub = $str_index + strlen($this->suffix);
        $str_index++;
        
        $tpl->setFile('plugin_topread', 'plugins/top_read.xml');
        $q = "SELECT p.id, p.post_name AS name, p.post_title AS title,
                UNIX_TIMESTAMP(p.post_date) AS tstamp, p.post_category, s.context,
                COUNT(*) AS hits,
                (COUNT(*) / DATEDIFF('{$this->_frontend->where_date}', p.post_date_gmt)) AS freq
              FROM {$this->_frontend->tables['stats']} s
              INNER JOIN {$this->_frontend->tables['posts']} p ON p.post_name=s.postname
              WHERE s.log_id='{$this->_frontend->options['shortname']}' 
                AND (s.context='post' OR s.context='static')
                AND p.post_date_gmt BETWEEN
                    '{$this->_frontend->where_date}' - interval {$this->active_days} day
                    AND '{$this->_frontend->where_date}' - interval 1 day
                AND (p.post_status='publish' OR p.post_status='static')
              GROUP BY p.post_name
              ORDER BY freq DESC
              LIMIT " . $this->limit;
        $db->query($q);
        
        if ($db->count == 0) {
            $tpl->setVar('PLUGIN_TOPREAD', ' ');
            return;
        }
        //die('<pre>'.print_r($db->all(), true).'</pre>');
        $date_format = (empty($this->date_format) ? $this->_frontend->options['short_date_format'] : $this->date_format);
        
        $top_read = array();
        foreach ($db->all() as $k => $v) {
            $v['permalink'] = ($v['context'] == 'post') ?
                    $this->_frontend->getPermalink($v['id'], $v['name'], $v['tstamp'], $v['post_category'])
                    : $this->_frontend->getPagePermalink($v['id'], $v['name']);
            $v['class'] = ($k % 2 == 0) ? 'sidebar_toplist_odd' : 'sidebar_toplist_even';
            $v['date'] = strftime($date_format, $v['tstamp']);
            $top_read[] = $v;
        }
        
        $tpl->parseBlock('PLUGIN_TOPREAD_POST', 'plugin_topread_post', $top_read, 'plugin_topread');
        $tpl->setVar('plugin_topread_active_days', $this->active_days);
        $tpl->parse('PLUGIN_TOPREAD', 'plugin_topread');
    }
    
}

?>